<%@ page language="java" contentType="text/javascript; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@page import='java.sql.*' %>
<%@page import="java.util.ArrayList"%>
<%@page import="net.sf.json.JSONObject"%>
<%@page import="net.sf.json.JSONSerializer"%>
<%@page import="net.sf.json.JSONArray"%>
<%@ page import="com.google.android.gcm.server.*" %>

<%
		
	response.setHeader("Access-Control-Allow-Origin", "*");
	response.setHeader("Access-Control-Allow-Headers", "origin, x-requested-with");
	request.setCharacterEncoding("UTF-8");
	String senderID = request.getParameter("senderID");
	String sender = request.getParameter("sender");
	String getterID = request.getParameter("getterID");
	String getter = request.getParameter("getter");
	String msg = request.getParameter("msg");
	//String msg = new String(request.getParameter("msg").getBytes("8859_1"), "utf-8");
	//msg = msg.replace("\n","<br>");
	//msg = msg.replace("\u0020","%nbsp;");
	String date = new java.text.SimpleDateFormat("yyyy-MM-dd").format(new java.util.Date());
	String time = new java.text.SimpleDateFormat("HH:mm:ss").format(new java.util.Date());
	
	Connection conn = null;
	PreparedStatement pstmt = null;
	PreparedStatement regpstmt = null;
	
	JSONObject jo = new JSONObject();
	
	try{
		Class.forName("com.mysql.jdbc.Driver");
		conn = DriverManager.getConnection(
			"jdbc:mysql://localhost/itscreater", "itscreater", "campus123");
		
		String insert = "insert into Message" +
						"(SenderID, Sender, GetterID, Getter, Message, Confirm, Date, Time)" +
						"values (?, ?, ?, ?, ?, ?, ?, ?);";
		pstmt = conn.prepareStatement(insert);
		pstmt.setString(1, "dd");
		pstmt.setString(2, sender);
		pstmt.setString(3, getterID);
		pstmt.setString(4, getter);
		pstmt.setString(5, msg);
		pstmt.setInt(6, 0);
		pstmt.setString(7, date);
		pstmt.setString(8, time);
		pstmt.executeUpdate();
		
		jo.put("success", "true");
		out.print(jo);
		
		String regidselect = "select * from gcm where ID = ?;";
		regpstmt = conn.prepareStatement(regidselect);
		regpstmt.setString(1, getterID);
		ResultSet rs = regpstmt.executeQuery();
		if (rs.next()) {
			String regid = rs.getString("regid");
			String push = rs.getString("Push");
			String vibrate = rs.getString("Vibrate");
			Sender gcmsender = new Sender("AIzaSyC71UD8FmXiaATQ1FUv6fQalDzkMv4qBpg");
			Message message = new Message.Builder()
					//.collapseKey(MESSAGE_ID)
					//.delayWhileIdle(SHOW_ON_IDLE)
					//.timeToLive(LIVE_TIME)
					.addData("push", push)
					.addData("vibrate", vibrate)
					.build();
			Result result = gcmsender.send(message, regid, 5);

			//if(result.getMessageId() != null) {}
				
			if (result.getErrorCodeName() == "NotRegistered") {
				String delete = "delete from gcm where ID = ?;";
				regpstmt = conn.prepareStatement(delete);
				regpstmt.setString(1, getterID);
				regpstmt.executeUpdate();
			}
		}
				
		
		
		
		
	}
	
	
	finally{
		try{
			pstmt.close();
		}
		catch (Exception ignored){
		}
		try{
			regpstmt.close();
		}
		catch (Exception ignored){
		}
		try{
			conn.close();
		}
		catch (Exception ignored){
		}
	}
%>